﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Hired
{
    public partial class question : System.Web.UI.Page
    {
        private string questionID;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null)
            {
                questionID = Request.QueryString["id"];

                Question q = DBQuestion.RetrieveQuestion(questionID, true);
                if (q != null)
                {
                    lblQuestionTitle.Text = q.Title;
                    lblQuestionContents.Text = q.Contents;
                    lblQuestionTimestamp.Text = q.Timestamp.ToString();

                    string accountType = DBAccount.RetrieveAccountTypeByID(q.UserID);
                    switch (accountType)
                    {
                        case "CompanyAccount":
                            CompanyAccount ca = DBCompanyAccount.RetrieveCompanyAccount(q.UserID, true);
                            lblPosterUsername.Text = ca.Username;
                            imgPoster.ImageUrl = ca.Company.ImageAddress;
                            break;
                        case "JobseekerAccount":
                            JobseekerAccount js = DBJobseekerAccount.retrieveJobseekerAccount(q.UserID, true);
                            lblPosterUsername.Text = js.Username;
                            imgPoster.ImageUrl = js.Jobseeker.ImageAddress;
                            break;
                    }

                    #region Answers
                    if (q.Answers.Count != 0)
                    {
                        for (int i = 0; i < q.Answers.Count; i++)
                        {
                            Answer a = q.Answers.ElementAt(i);
                            UserControl UCAnswer = (UserControl)LoadControl("UCAnswers.ascx");
                            pnlAnswers.Controls.Add(UCAnswer);

                            // accessing UCAnswer controls
                            LinkButton lblAnswerPosterUsername = (LinkButton)UCAnswer.FindControl("lblPosterUsername");
                            Label lblAnswerCounter = (Label)UCAnswer.FindControl("lblCounter");
                            Label lblAnswerTimestamp = (Label)UCAnswer.FindControl("lblTimestamp");
                            Label lblAnswerContents = (Label)UCAnswer.FindControl("lblAnswerContents");
                            Label lblAnswerRatingPositive = (Label)UCAnswer.FindControl("lblRatingPositive");
                            Label lblAnswerRatingNegative = (Label)UCAnswer.FindControl("lblRatingNegative");
                            ImageButton imgAnswerPoster = (ImageButton)UCAnswer.FindControl("imgPoster");

                            // assigning values
                            lblAnswerCounter.Text += (i + 1).ToString();
                            lblAnswerTimestamp.Text = a.Timestamp.ToString();
                            lblAnswerContents.Text = a.Contents;
                            lblAnswerRatingPositive.Text = a.PositiveRating.ToString();
                            lblAnswerRatingNegative.Text = a.NegativeRating.ToString();

                            accountType = DBAccount.RetrieveAccountTypeByID(a.UserID);
                            switch (accountType)
                            {
                                case "CompanyAccount":
                                    CompanyAccount ca = DBCompanyAccount.RetrieveCompanyAccount(q.UserID, true);
                                    lblAnswerPosterUsername.Text = ca.Username;
                                    imgAnswerPoster.ImageUrl = ca.Company.ImageAddress;
                                    break;
                                case "JobseekerAccount":
                                    JobseekerAccount js = DBJobseekerAccount.retrieveJobseekerAccount(q.UserID, true);
                                    lblAnswerPosterUsername.Text = js.Username;
                                    imgAnswerPoster.ImageUrl = js.Jobseeker.ImageAddress;
                                    break;
                            }
                        }
                    }
                    #endregion
                }

                if (!User.Identity.IsAuthenticated || q.UserID == User.Identity.Name || q.Answers.Any(a => a.UserID == User.Identity.Name))
                {
                    pnlPostAnswer.Visible = false;
                }
            }
            else
            {
                Response.Redirect("missingquestion.aspx");
            }
        }

        protected void btnAnswer_Click(object sender, EventArgs e)
        {
            Answer a = new Answer();
            a.Contents = txtAnswer.Text;
            a.UserID = User.Identity.Name;
            a.Timestamp = DateTime.Now;
            a.QuestionID = questionID;
            DBAnswer.AddAnswer(a);
        }
    }
}